package com.yang.o2o.service.impl;

import com.yang.o2o.dao.ShopDao;
import com.yang.o2o.dto.ShopExecution;
import com.yang.o2o.dto.ShopStateEnum;
import com.yang.o2o.entity.Shop;
import com.yang.o2o.exceptions.ShopOperationException;
import com.yang.o2o.service.ShopService;
import com.yang.o2o.util.ImageUtil;
import com.yang.o2o.util.PageCalculator;
import com.yang.o2o.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopDao shopDao;

    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex,pageSize);
        List<Shop> shopList = shopDao.queryShopList(shopCondition,rowIndex,pageSize);
        int shopCount = shopDao.queryShopCount(shopCondition);
        ShopExecution se = new ShopExecution();
        if(shopList != null ){
            se.setShopList(shopList);
            se.setCount(shopCount);
        }else{
            se.setState(ShopStateEnum.INNER_ERROR.getState());
        }
        return se;
    }

    @Transactional
    public ShopExecution addShop(Shop shop, InputStream shopImg,String fileName) {
        //空值判断
        if(shop == null){
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }else {
            try {
                //默认的值
                shop.setEnableStatus(0);//审核中
                shop.setCreateTime(new Date());
                shop.setLastEditTime(new Date());

                int effectdNum = shopDao.insertShop(shop);
                //默认信息是否添加成功
                if (effectdNum <= 0) {
                    throw new ShopOperationException("店铺创建失败");
                } else {
                    if (shopImg != null) {
                        //存储图片
                        try {
                            addShopImg(shop, shopImg,fileName);
                        } catch (Exception e) {
                            throw new ShopOperationException("addShopImg error:" + e.getMessage());
                        }
                        int effectNum = shopDao.updateShop(shop);
                        if (effectNum <= 0) {
                            throw new ShopOperationException("更新图片地址失败");
                        }
                    }
                }
            } catch (Exception e) {
                throw new ShopOperationException("addShop error:" + e.getMessage());
            }
        }
        return new ShopExecution(ShopStateEnum.CHECK);
    }

    public Shop getByShopId(long shopId) {
        return shopDao.queryByShopId(shopId);
    }

    public ShopExecution modifyShop(Shop shop, InputStream shopImgInputStream, String fileName) {
        if(shop == null || shop.getShopId() == null){
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }else{
        try{
            // 1.判断是否需要修改图片
            if(shopImgInputStream != null){
                Shop tempShop = shopDao.queryByShopId(shop.getShopId());
                if(tempShop.getShopImg() != null && fileName != null && "".equals(fileName)){
                    ImageUtil.deleteFileOrPath(tempShop.getShopImg());
                }
                addShopImg(shop,shopImgInputStream,fileName);
            }
            // 2.更新
            shop.setLastEditTime(new Date());
            int effectedNum = shopDao.updateShop(shop);
            if(effectedNum <= 0){
                return new ShopExecution(ShopStateEnum.INNER_ERROR);
            }else{
                shop = shopDao.queryByShopId(shop.getShopId());
                return new ShopExecution(ShopStateEnum.SUCCESS);
            }
        }catch (Exception e){
            throw new ShopOperationException("modifyShop异常:" + e.getMessage());
        }
        }

    }

    private void addShopImg(Shop shop, InputStream shopImg,String fileName) {
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImageUtil.generateThumbnail(shopImg,fileName,dest);
        System.out.println(shopImgAddr);
        shop.setShopImg(shopImgAddr);
    }


}
